package com.loop.unclelend.ui.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Typeface
import android.os.Handler
import android.os.Looper
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UiPageApplyWaitBinding
import com.loop.unclelend.pro.ProActivity
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import com.loop.unclelend.utils.thread.TimingThread

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/26 18:45
 * @Annotation：申请贷款等待
 */
class Act_Apply_Wait: ProActivity(), TimingThread.ITimingThreadListener {
    lateinit var binding: UiPageApplyWaitBinding
    private var timeThread: TimingThread? = null
    //等待时长
    private var countdown = 9

    override fun getUncleRootView(): View {
        binding = UiPageApplyWaitBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initUncleView() {
        setUncleTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setUncleTextTitle("")
        setUncleBackBtnVisibility(View.INVISIBLE)
        setUncleRightBtnRes(R.mipmap.icon_kefu)

        val textSMS = getString(R.string.loan_apply_wait_sms)
        val showText = String.format(getString(R.string.loan_apply_wait_tips),textSMS)

        val smsIndex = showText.indexOf(textSMS)
        val spString = SpannableString(showText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FFFF0000")), smsIndex, smsIndex + textSMS.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spString.setSpan(StyleSpan(Typeface.BOLD), smsIndex, smsIndex + textSMS.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        binding.textWaitTips.text = spString
    }

    override fun initUncleListener() {
        //联系客服
        rootViewBinding.imgKefu.setOnClickListener {
            showUncleKefuDialog("Loan SMS Preview Page")
            UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_CUSTOMER,"Loan SMS Preview Page")
        }
    }

    override fun initUncleData() {
        startTimeThread()
    }

    /**
     * 启动倒计时
     */
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        //随机 10秒 等待时长
        countdown = 9
        showUncleTipsText()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    override fun onLoading() {
        if (countdown <= 1){
            countdown = 9
            timeThread!!.stopThread()
            showUncleLoading(isTOuchOut = false, isCancel = true)
            showUncleLoanApplySuccess()
            return
        }
        countdown--
        showUncleTipsText()
    }

    /**
     * 设置文本
     */
    @SuppressLint("SetTextI18n")
    private fun showUncleTipsText(){
        binding.txtCountdown.text = "${countdown}s"
        showUncleStepStatusView()
    }

    /**
     * 贷款订单创建成功
     */
    private fun showUncleLoanApplySuccess(){
        Handler(Looper.getMainLooper()).postDelayed({
            hideUncleLoading()
            //返回首页
            finish()
        },500)
    }

    /**
     * 显示状态
     */
    private fun showUncleStepStatusView(){
        if (countdown in 7..9){
            binding.imgStep1.setImageResource(R.mipmap.icon_wait_step2)
            binding.txtStep1.setTextColor(Color.parseColor("#FF000000"))

            binding.imgStep2.setImageResource(R.mipmap.icon_wait_countdown)
            binding.txtStep2.setTextColor(Color.parseColor("#FF000000"))

            binding.imgStep3.setImageResource(R.mipmap.icon_wait_countdown)
            binding.txtStep3.setTextColor(Color.parseColor("#FF000000"))
            return
        }

        if (countdown in 4..6){
            binding.imgStep1.setImageResource(R.mipmap.icon_wait_step1)
            binding.txtStep1.setTextColor(Color.parseColor("#FFA9A9AC"))

            binding.imgStep2.setImageResource(R.mipmap.icon_wait_step2)
            binding.txtStep2.setTextColor(Color.parseColor("#FF000000"))

            binding.imgStep3.setImageResource(R.mipmap.icon_wait_countdown)
            binding.txtStep3.setTextColor(Color.parseColor("#FF000000"))
            return
        }
        binding.imgStep1.setImageResource(R.mipmap.icon_wait_step1)
        binding.txtStep1.setTextColor(Color.parseColor("#FFA9A9AC"))

        binding.imgStep2.setImageResource(R.mipmap.icon_wait_step1)
        binding.txtStep2.setTextColor(Color.parseColor("#FFA9A9AC"))

        binding.imgStep3.setImageResource(R.mipmap.icon_wait_step2)
        binding.txtStep3.setTextColor(Color.parseColor("#FF000000"))
    }

    override fun onStart() {
        super.onStart()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_LOAN_SMS_PREVIEW_START, "")
    }

    override fun onDestroy() {
        super.onDestroy()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_LOAN_SMS_PREVIEW_END, "")
        if (timeThread != null) {
            timeThread!!.stopThread()
            timeThread = null
        }
    }
}